package nc.ui.dm.m4804.action;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import nc.bs.framework.common.NCLocator;
import nc.itf.uap.IVOPersistence;
import nc.ui.pubapp.uif2app.actions.DeleteAction;
import nc.ui.uif2.model.IMultiRowSelectModel;
import nc.vo.dm.enumeration.FstatusFlag;
import nc.vo.dm.m4804.ImportVO;
import nc.vo.dm.m4804.entity.DelivBillAggVO;
import nc.vo.dm.m4804.entity.DelivBillHVO;
import nc.vo.pub.lang.UFBoolean;


public class DelivBillDeleteAction extends DeleteAction {
	private static final long serialVersionUID = 1L;

	public DelivBillDeleteAction() {}

	protected boolean isActionEnable() {
		boolean enable = super.isActionEnable();
		if (enable) {
			Object[] data = ((IMultiRowSelectModel)getModel()).getSelectedOperaDatas();
			if ((data != null) && (data.length > 1)) {
				return true;
			}
			DelivBillAggVO bill = (DelivBillAggVO)getModel().getSelectedData();
			Integer fstatusflag = bill.getParentVO().getFstatusflag();
			UFBoolean bapsettledflag = bill.getParentVO().getBapsettledflag();
			if ((FstatusFlag.FREE.equalsValue(fstatusflag)) && (!bapsettledflag.booleanValue())) {
				return true;
			}
			return false;
		}
		return enable;
	}

	public void doAction(ActionEvent e) throws Exception {
		Object[] data = ((IMultiRowSelectModel)getModel()).getSelectedOperaDatas();
		List<String> delkey = new ArrayList<String>();
		super.doAction(e);
		for(Object bill : data){
			DelivBillHVO head = ((DelivBillAggVO)bill).getParentVO();
			String vtrantypecode = head.getVtrantypecode();
			String pk_timekey = head.getVdef20();
			if("4804-Cxx-001".equals(vtrantypecode) && pk_timekey != null){
				if(delkey.size() == 0){
					NCLocator.getInstance().lookup(IVOPersistence.class).deleteByClause(ImportVO.class, "pk_timekey = '" + pk_timekey + "'");
					delkey.add(pk_timekey);
				} else {
					for(String key : delkey){
						if(!key.equals(pk_timekey)){
							NCLocator.getInstance().lookup(IVOPersistence.class).deleteByClause(ImportVO.class, "pk_timekey = '" + pk_timekey + "'");
							delkey.add(pk_timekey);
							break;
						}
					}
				}
			}

		}
	}


}